//
//  QNGridItem.m
//  TestZaker
//
//  Created by gang.xu on 13-4-25.
//  Copyright (c) 2013年 去哪儿. All rights reserved.
//

#import "QNGridItem.h"

#define kQNGridItemDeleteButtonSize     20
#define kQNGridItemDeleteButtonSpace    5
#define kQNGridItemDefaultBGColor       [UIColor colorWithRed:0 green:0 blue:1 alpha:0.4]
#define kQNGridItemDeleteBtnImage       @"deletbutton.png"

@implementation QNGridItem

// 工厂方法
+ (id)allocWithFrame:(CGRect)frame delegate:(id<QNGridItemDelegate>)delegate title:(NSString *)title bgImage:(UIImage *)bgImage {
    QNGridItem *gridItem = [[QNGridItem alloc] initWithFrame:frame delegate:delegate title:title bgImage:bgImage];
    return gridItem;
}

// 初始化控件
- (id)initWithFrame:(CGRect)frame delegate:(id<QNGridItemDelegate>)delegate title:(NSString *)title bgImage:(UIImage *)bgImage;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.isRemovable = NO;
        _delegate = delegate;
        _isEditing = NO;
        
        // 主按钮
        _mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mainButton setFrame:self.bounds];
        if (bgImage) {
            [_mainButton setBackgroundColor:[UIColor clearColor]];
            [_mainButton setBackgroundImage:bgImage forState:UIControlStateNormal];
        }
        else {
            [_mainButton setBackgroundColor:kQNGridItemDefaultBGColor];
        }
        [_mainButton setTitle:title forState:UIControlStateNormal];
        [_mainButton addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_mainButton];
        
        // 注册长按事件
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doLongPress:)];
        [self addGestureRecognizer:longPress];
        [longPress release];
        
        // 删除按钮
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setFrame:CGRectMake(kQNGridItemDeleteButtonSpace, kQNGridItemDeleteButtonSpace, kQNGridItemDeleteButtonSize, kQNGridItemDeleteButtonSize)];
        [_deleteButton setBackgroundColor:[UIColor clearColor]];
        UIImage *deleteBtnImage = [UIImage imageNamed:kQNGridItemDeleteBtnImage];
        if (deleteBtnImage) {
            [_deleteButton setImage:deleteBtnImage forState:UIControlStateNormal];
        }
        [_deleteButton addTarget:self action:@selector(removeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_deleteButton setHidden:YES];
        [self addSubview:_deleteButton];
    }
    return self;
}

- (void)dealloc
{
    [_icon release];
    [_title release];
    [_bgImage release];
    [_mainButton release];
    [_deleteButton release];
    [super dealloc];
}

#pragma mark - Custom Methods

// 设置编辑状态
- (void)enableEditing {
    if (_isEditing == YES)
        return;
    
    // put item in editing mode
    _isEditing = YES;
    
    // make the remove button visible
    [_deleteButton setHidden:!self.isRemovable];
    [_mainButton setEnabled:NO];
}

// 重置编辑状态
- (void)disableEditing {
    [_deleteButton setHidden:YES];
    [_mainButton setEnabled:YES];
    _isEditing = NO;
}

#pragma mark - Actions

// 点击主控件响应方法
- (void)clickItem:(id)sender {
    [_delegate gridItemDidClicked:self];
}

// 点击删除按钮响应方法
- (void)removeButtonClicked:(id)sender  {
    [_delegate gridItemDidDeleted:self atTag:self.tag];
}

// 手势响应方法
- (void)doLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    
    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan: {
            // 放大
            [UIView beginAnimations:@"imageViewBig" context:nil];
            [UIView setAnimationDuration:0.2];
            CGAffineTransform newTransform =  CGAffineTransformScale(self.transform, 1.1, 1.1);
            [self setTransform:newTransform];
            [UIView commitAnimations];
            // 回调代理方法
            _point = [gestureRecognizer locationInView:self];
            [_delegate gridItemDidEnterEditingMode:self];
            break;
        }
        case UIGestureRecognizerStateEnded: {
            // 变回原来大小
            [UIView beginAnimations:@"imageViewSmall" context:nil];
            [UIView setAnimationDuration:0.2];
            CGAffineTransform newTransform = CGAffineTransformConcat(self.transform,  CGAffineTransformInvert(self.transform));
            [self setTransform:newTransform];
            self.center = CGPointMake((self.frame.origin.x + self.frame.size.width / 2.0), (self.frame.origin.y + self.frame.size.height / 2.0));
            [UIView commitAnimations];
            // 回调代理方法
            _point = [gestureRecognizer locationInView:self];
            [_delegate gridItemDidEndMoved:self withLocation:_point moveGestureRecognizer:gestureRecognizer];
            break;
        }
        case UIGestureRecognizerStateFailed: {
            break;
        }
        case UIGestureRecognizerStateChanged: {
            // 回调代理方法，移动控件
            [_delegate gridItemDidMoved:self withLocation:_point moveGestureRecognizer:gestureRecognizer];
            break;
        }
        default: break;
    }
}

# pragma mark - Overriding UIView Methods

// 删除控件设置动画
- (void) removeFromSuperview {
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.0;
        [self setFrame:CGRectMake(self.frame.origin.x+50, self.frame.origin.y+50, 0, 0)];
        [_deleteButton setFrame:CGRectMake(0, 0, 0, 0)];
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
}

@end
